package com.supermap.dataservice.project.template;

import com.supermap.dubbo.api.cq.product.agri.AgriProduct;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年11月08 下午 6:25
 * @email 1301584033@qq.com
 */
@Mapper
public interface ProductTemplateMapper {

    /**
     *产品组
     * */
//    @Select("select tt1.*,tt2.depareacode,if(tt2.depareacode =#{areaCode},0,1) as state  from ( select t1.id,t1.group_name as groupName,t1.production_unitid,date_format(t1.create_date,'%Y-%m-%d %H:%i:%S') as createDate from t_b_productgroup t1   \n" +
//            "where position(#{areaCode} in fit_areaid ) and t1.content_category=#{templateId} ) tt1 \n" +
//            "            inner join t_s_depart tt2 on tt1.production_unitid=tt2.id\n")
    @Select("select t1.id,t1.production_unitid,if(t2.depareacode <> #{areaCode},1,0) state,\n" +
            "if(t2.depareacode <> #{areaCode},if(position(left(t2.departname,2) in group_name),concat(group_name,'(共享)'),concat(replace(departname,'气象局',''),group_name,'(共享)')),if(position(left(t2.departname,2) in group_name),group_name,concat(replace(departname,'气象局',''),group_name)) ) groupName,\n" +
            "date_format(t1.create_date,'%Y-%m-%d %H:%i:%S') as createDate from t_b_productgroup t1 \n" +
            "inner join t_s_depart t2 on find_in_set(t2.ID,t1.production_unitid)\n" +
            "inner join t_b_pms_product t3 on t1.id  = t3.product_group\n" +
            "where find_in_set(#{areaCode} ,fit_areaid) and t1.content_category = #{templateId} and t3.product_state = 6\n" +
            "group by t1.id\n" +
            "having count(t3.id)> 0\n" +
            "order by state\n")
    List<Map<String,Object>> getProductGrupByTemplateId(@Param("areaCode")Integer areaCode, @Param("templateId")Integer templateId);

    @Select("SELECT id as templateId,name as templateName FROM t_producttemplate WHERE DEL_FLG=0")
    List<Map<String,Object>> ggetListProductTemplate();


    @Select("SELECT id as templateId,name as templateName FROM t_producttemplate WHERE DEL_FLG=0")
    List<AgriProduct> ggetListProductTemplateAgriProduct();
}
